# Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
# All rights reserved.
#
# This software may be modified and distributed under the terms of the
# BSD-3-Clause license. See the accompanying LICENSE file for details.

macro(add_yarpidl_thrift_test name wd file check_file)
  # FIXME directory is not created automatically by yarpidl_thrift
  add_test(NAME idl::thrift::${name}::setup
           COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/${name}")
  set_tests_properties(idl::thrift::${name}::setup PROPERTIES FIXTURES_SETUP yarpidl_thrift_${name}_setup)

  # Run the test
  add_test(NAME idl::thrift::${name}::run
           COMMAND yarpidl_thrift --out "${CMAKE_CURRENT_BINARY_DIR}/${name}" --gen yarp "${file}"
           WORKING_DIRECTORY "${wd}")
  set_tests_properties(idl::thrift::${name}::run PROPERTIES FIXTURES_REQUIRED yarpidl_thrift_${name}_setup)
  set_tests_properties(idl::thrift::${name}::run PROPERTIES FIXTURES_SETUP yarpidl_thrift_${name}_run)
  set_tests_properties(idl::thrift::${name}::run PROPERTIES LABELS "yarp::idl::thrift")

  # Check if the file was generated by trying to generate md5sum
  add_test(NAME idl::thrift::${name}::check
           COMMAND ${CMAKE_COMMAND} -E md5sum "${CMAKE_CURRENT_BINARY_DIR}/${name}/${check_file}"
           WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/demo")
  set_tests_properties(idl::thrift::${name}::check PROPERTIES FIXTURES_REQUIRED yarpidl_thrift_${name}_run)
  set_tests_properties(idl::thrift::${name}::check PROPERTIES LABELS "yarp::idl::thrift")

  # Cleanup
  add_test(NAME idl::thrift::${name}::cleanup
           COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/${name}")
  set_tests_properties(idl::thrift::${name}::cleanup PROPERTIES FIXTURES_CLEANUP "yarpidl_thrift_${name}_setup;yarpidl_thrift_${name}_run")
  set_tests_properties(idl::thrift::${name}::cleanup PROPERTIES LABELS "yarp::idl::thrift")

  set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${CMAKE_CURRENT_BINARY_DIR}/${name}")
endmacro()


# Try generating the files using different combinations of current working
# directory and path to the file
add_yarpidl_thrift_test(test1 "${CMAKE_CURRENT_SOURCE_DIR}/demo" demo.thrift Demo.h)
add_yarpidl_thrift_test(test2 "${CMAKE_CURRENT_SOURCE_DIR}" demo/demo.thrift Demo.h)
add_yarpidl_thrift_test(test3 "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/demo/demo.thrift" Demo.h)


# Test yarp_idl_to_dir and yarp_add_idl
add_test(NAME idl::thrift::demo::run
         COMMAND ${CMAKE_CTEST_COMMAND} -VV
                                        --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/demo"
                                                         "${CMAKE_CURRENT_BINARY_DIR}/demo"
                                        --build-two-config
                                        ${build_generator}
                                        --build-project demo
                                        --build-options ${build_options}
                                                        ${build_module_path}
                                                        -DYARP_DIR=${CMAKE_BINARY_DIR}
                                                        -DALLOW_IDL_GENERATION=TRUE
                                        --test-command ${CMAKE_CTEST_COMMAND} ${build_generator}
         WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
set_tests_properties(idl::thrift::demo::run PROPERTIES FIXTURES_SETUP yarpidl_thrift_demo_run)
set_tests_properties(idl::thrift::demo::run PROPERTIES LABELS "yarp::idl::thrift")

# Cleanup
add_test(NAME idl::thrift::demo::cleanup
         COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/demo")
set_tests_properties(idl::thrift::demo::cleanup PROPERTIES FIXTURES_CLEANUP yarpidl_thrift_demo_run)
set_tests_properties(idl::thrift::demo::cleanup PROPERTIES LABELS "yarp::idl::thrift")

set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${CMAKE_CURRENT_BINARY_DIR}/demo")
